package com.penghaisoft.microwms.producthand.web.controller.outwarehouse;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.ProScrapOutSnDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.common.wms.model.ProScrapOutSn;
import com.penghaisoft.microwms.product.service.IProScrapOutSnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/proScrapOutSn")
@Slf4j
public class ProScrapOutSnController extends BaseController {

    @Autowired
    private IProScrapOutSnService proScrapOutSnService;

    @Autowired
    private IWmsCommonService wmsCommonService;

    /**
     * 新增记录
     *
     * @param proScrapOutSn
     * @return
     */
    @PostMapping("c")
    public TransResult create(ProScrapOutSn proScrapOutSn) {
        proScrapOutSn.preInsert();
        Resp resp = proScrapOutSnService.create(proScrapOutSn);
        return fromResp(resp);
    }

    /**
     * 删除记录（逻辑删除active_flag=0)
     *
     * @param proScrapOutSn
     * @return
     */
    @PostMapping("d")
    public TransResult delete(ProScrapOutSn proScrapOutSn) {
        proScrapOutSn.preUpdate();
        Resp resp = proScrapOutSnService.delete(proScrapOutSn);
        return fromResp(resp);
    }

    /**
     * 查询列表
     *
     * @param
     * @return
     */
    @PostMapping("list")
    public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProScrapOutSn proScrapOutSn) {
        Pager<ProScrapOutSn> result = proScrapOutSnService.findListByCondition(page, rows, proScrapOutSn);
        return success(result);
    }

    /**
     * 查询单条
     *
     * @param id
     * @return
     */
    @GetMapping("r/{id}")
    public TransResult queryById(@PathVariable String id) {
        ProScrapOutSn proScrapOutSn = proScrapOutSnService.findById(id);
        return success(proScrapOutSn);
    }

    /**
     * 修改记录
     *
     * @param proScrapOutSn
     * @return
     */
    @PostMapping("u")
    public TransResult update(ProScrapOutSn proScrapOutSn) {
        proScrapOutSn.preUpdate();
        Resp resp = proScrapOutSnService.update(proScrapOutSn);
        return fromResp(resp);
    }

    /**
     * wzhg 报废出库扫码
     * 入参：条码、报废原因、登录人
     * @return
     */
    @PostMapping("scan")
    public HandResp scan(@RequestBody ProScrapOutSnDto proScrapOutSnDto) {
        HandResp resp = new HandResp();

        String checkInNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_BF, Constant.SEQ_PREFIX_SHORT_REDIS_PRO_XSC, 1).get(0);
        // udf2 暂存批次号
        proScrapOutSnDto.setUserDefined2(checkInNo);
        resp = proScrapOutSnService.scan(proScrapOutSnDto);

        return resp;
    }

}
